package com.crl.rcdp.sys.controller;

import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.DeleteValidGroup;
import com.crl.rcdp.sys.dto.SysRoleDTO;
import com.crl.rcdp.sys.model.SysRoleResource;
import com.crl.rcdp.sys.service.SysRoleService;
import com.crl.rcdp.sys.vo.SysRoleSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/21 17:04
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取所有角色信息
     */
    @PostMapping("/list")
    @ApiOperation(value = "角色列表")
    public JsonResult list(@RequestBody SysRoleSearchVO sysRoleSearchVO) {
        return JsonResultUtil.ok(sysRoleService.page(sysRoleSearchVO));
    }


    /**
     * 新增角色
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public JsonResult add(@RequestBody SysRoleDTO sysRoleDTO) {
        int flag = sysRoleService.add(sysRoleDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 修改角色
     *
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改角色")
    public JsonResult update(@RequestBody SysRoleDTO sysRoleDTO) {
        int flag = sysRoleService.update(sysRoleDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除角色
     *
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    public JsonResult delete(@Validated({DeleteValidGroup.class}) @RequestBody SysRoleDTO sysRoleDTO) {
        int flag = sysRoleService.delete(sysRoleDTO.getId());
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取所有角色信息
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个角色")
    public JsonResult get(@RequestBody SysRoleSearchVO sysRoleSearchVO) {
        SysRoleDTO sysRoleDTO = sysRoleService.get(sysRoleSearchVO.getId());
        List<SysRoleResource> list = sysRoleService.listRoleResources(sysRoleSearchVO.getId());
        StringBuffer sb = new StringBuffer();
        for (SysRoleResource sysRoleResource : list) {
            if (StringUtil.isNotNullOrEmpty(sysRoleResource.getPath()) && sysRoleResource.getPath().length() > 1) {
                sb.append(sysRoleResource.getResourceId()).append("@");
            }
        }
        sysRoleDTO.setResourceIds(sb.toString());//设置已经授权的资源
        return JsonResultUtil.ok(sysRoleDTO);
    }
}
